package com.wmzdq.aiservice.controller;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.alibaba.cloud.ai.memory.jdbc.MysqlChatMemoryRepository;
import com.alibaba.cloud.ai.toolcalling.time.GetTimeByZoneIdService;
import com.wmzdq.aiservice.service.RecordTools;
import com.wmzdq.aiservice.service.TimeTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/baby")
public class BabyController {


   // private final ChatClient chatClient;

    private final ChatClient noMemoryChatClient;
    private final int MAX_MESSAGES = 100;
    private final MessageWindowChatMemory messageWindowChatMemory;

    @Autowired
    RecordTools recordtool;
    TimeTools timeTools=new TimeTools(new GetTimeByZoneIdService());




    public BabyController(JdbcTemplate jdbcTemplate,@Qualifier("dashscopeChatModel") DashScopeChatModel chatModel) {
        ChatMemoryRepository chatMemoryRepository = MysqlChatMemoryRepository.mysqlBuilder()
                .jdbcTemplate(jdbcTemplate)
                .build();
        this.messageWindowChatMemory = MessageWindowChatMemory.builder()
                .chatMemoryRepository(chatMemoryRepository)
                .maxMessages(MAX_MESSAGES)
                .build();
//        this.chatClient = builder
//                .defaultAdvisors(
//                        MessageChatMemoryAdvisor.builder(messageWindowChatMemory)
//                                .build(),new SimpleLoggerAdvisor()
//                ).defaultOptions(
//                        DashScopeChatOptions.builder()
//                                .withTopP(1.0)
//                                .build()
//                )
//                .build();

        this.noMemoryChatClient = ChatClient.builder(chatModel)
                .defaultAdvisors(
                       new SimpleLoggerAdvisor()
                ).defaultOptions(
                        DashScopeChatOptions.builder()
                                .withTopP(0.8)
                                .build()
                )
                .build();

    }


    @RequestMapping("/handle")
    public String handle(@RequestParam("query") String query) {
        //SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String text=noMemoryChatClient.prompt().system("请根据北京时间和小孩的日常作息，解答疑惑,或者记录日常活动，如果调用了外部工具，请求在metadata中返回调用信息").user("现在的时间，"+query+",如果调用了外部工具，请求在metadata中返回调用信息")
                .tools(recordtool).tools(timeTools).call().content();
        if(text.contains("已经记录")||text.contains("已记录")||text.contains("记录好了")) {
            return "{\"text\":\"记录好了\"}";
        }
        return "{\"text\":\"记录失败\"}";
    }
}
